package br.com.viamais.model;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import br.com.viamais.enumerator.Permission;
import br.com.viamais.infra.model.BaseEntity;

/**
 * <B>Project: viamais-web</B><BR>
 * 
 * Class that represents the Role entity.
 * 
 * <DL>
 * <DT><B>Created:</B>
 * <DD>Oct 3, 2011</DD></DT>
 * </DL>
 * <DL>
 * <DT><B>Last Modification:</B>
 * <DD>Oct 3, 2011</DD></DT>
 * </DL>
 * 
 * @author Caio Carleto - CarSer - Oct 3, 2011
 * @version 1.0
 */
@Entity
@Table(name = "tb_role")
public class Role implements BaseEntity {

	/**
	 * Serial version ID.
	 */
	private static final long serialVersionUID = -9007621583277529343L;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "id_role", unique = true, nullable = false)
	private Integer id;
	/**
	 * Permission name.
	 */
	@Column(name = "cl_permission", nullable = false, unique = true, updatable = true)
	@Enumerated(EnumType.STRING)
	private Permission permission;
	
	/**
	 * Profile List.
	 */
	@ManyToMany(mappedBy="roles")
	private List<Profile> profiles;

	/**
	 * Default Constructor.
	 */
	public Role() {
		super();
	}
	/**
	 * Constructor with fields.
	 * @param permission
	 */
	public Role(Permission permission) {
		this.permission = permission;
	}
	/**
	 * TO set id
	 * @param id
	 */
	public void setId(Integer id) {
		this.id = id;
	}

	@Override
	public Integer getId() {
		return this.id;
	}

	/**
	 * @return the profiles
	 */
	public List<Profile> getProfiles() {
		return profiles;
	}

	/**
	 * @param profiles
	 *            the profiles to set
	 */
	public void setProfiles(List<Profile> profiles) {
		this.profiles = profiles;
	}
	
	/**
	 * Delete profiles.
	 */
	public void deleteProfiles() {
		if(this.profiles != null) {
			for (Profile profile : profiles) {
				profile.setRoles(null);
			}
			this.setProfiles(null);
		}
	}
	
	/**
	 * @param profile
	 */
	public void addProfile(Profile profile) {
		this.profiles.add(profile);
	}

	/**
	 * To get permissions.
	 * @return
	 */
	public Permission getPermission() {
		return permission;
	}

	/**
	 * To set permissions
	 * @param permission
	 */
	public void setPermission(Permission permission) {
		this.permission = permission;
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((permission == null) ? 0 : permission.hashCode());
		return result;
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof Role)) {
			return false;
		}
		Role other = (Role) obj;
		if (permission == null) {
			if (other.permission != null) {
				return false;
			}
		} else if (!permission.equals(other.permission)) {
			return false;
		}
		return true;
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "Permission [permission=" + permission + "]";
	}

}
